****************************
* Peer Group Regressions
****************************

use "transactions.dta"
set more off

gen lnpic = ln(picnumtotal+1)
rename sizechoice sizechoicenum
gen lnsize = ln(sizechoicenum)
gen lnitem = ln(itemnum)
gen lnword = ln(wordnum+1)
gen decoration = 1 if itemdecoration!=""
replace decoration = 0 if strpos(itemdecoration,"None")
replace decoration=0 if itemdecoration==""

//weekly variable
gen week = week(dofc(time))
gen year = year(dofc(time))
replace year = year - 2014
replace week = year*52+ week
gen rateind =(buyerrating!=.)

bys productid week: egen weeklyrev = sum(transactionvalue)
bys productid week: egen weeklyquan = sum(quantityinpiece)
bys productid week: egen weeklybnum = nvals(buyerid)
bys productid week: egen weeklydnum = nvals(buyercountry)
gen tranprice = transactionvalue/quantityinpiece
gsort +productid +week +time
by productid week: gen first = _n==1
gen wprice = tranprice if first==1
bys productid week: egen wtranprice = mean(wprice)
gen lnwtranprice = ln(wtranprice)

bys productid week: egen weeklyratenum = sum(rateind)
bys productid week: egen weeklyratesum = sum(buyerrating)
bys productid week: gen weeklyordernum = _N

// weekly star
foreach i of numlist 1/5{
	gen star`i' = 1 if buyerrating == `i'
	bys productid week: egen star`i'num = sum(star`i')
}
//
duplicates drop productid week, force


// fillin, try product fe first
preserve
tempfile tempquality
local qualityvar "materialquality fguapro freturnpolicy lnsize lnitem lnpic sellerid productrating"
keep productid `qualityvar'
duplicates drop productid, force
save `tempquality',replace
restore

bys productid: egen entryweek = min(week)
replace entryweek = 5 if prevordernum!=0
fillin productid week
bys productid: egen entryweeknew = min(entryweek)
drop entryweek
rename entryweeknew entryweek
drop if week<entryweek
local qualityvar "materialquality fguapro freturnpolicy lnsize lnitem lnpic sellerid productrating"
drop `qualityvar'
merge m:1 productid using `tempquality'
drop _merge
merge m:1 productid using `similar'
drop if _merge==2
drop _merge
gen pgroup_use = productg
replace pgroup_use = productid if pgroup_use==.

xtset productid week
replace lnwtranprice = l.lnwtranprice if lnwtranprice==.
// fillin end here
*

local weeklyvar "weeklyordernum weeklyratenum weeklyrev weeklyquan weeklybnum weeklydnum weeklyratesum star1num star2num star3num star4num star5num"
foreach k of local weeklyvar{
	replace `k'=0 if `k'==.
	}
foreach k of numlist 1/5 {
	gen cumstar`k' = 0
	foreach i of numlist 1/24 {
		gen l`i'star`k' = l`i'.star`k'num
		replace l`i'star`k' = 0 if l`i'star`k'==.
		replace cumstar`k' = cumstar`k' + l`i'star`k'
		}
	}
gen ave_rating = (cumstar1*1+cumstar2*2+cumstar3*3 + cumstar4*4 + cumstar5*5)/(cumstar1 + cumstar2 + cumstar3 + cumstar4 + cumstar5)
recode ave_rating (1/1.999999999 = 1) (2/2.999999999 = 2) (3/3.999999999999=3) (4/5 = 4) (else = 0), gen(cat_rate)
qui tab cat_rate, gen(cat_ratedum)
drop cat_ratedum2 // reference group is no rating

egen ratingave = mean(ave_rating)
gen rating_ad = ave_rating/ratingave
gen rated = (ave_rating!=.)
gen rated_ad = rating_ad
replace rated_ad = 0 if rated_ad==.
gsort +productid +week
by productid: gen totalorder = sum(weeklyordernum) 
by productid: gen totalratenum = sum(weeklyratenum) //rate num not adjusted by previous orders
by productid: gen totalrate = sum(weeklyratesum)
//by productid: egen entryweek = min(week)
replace totalorder = totalorder + prevordernum
gen lntotalordernum = ln(totalorder)

gen lnweeklyordernum = ln(weeklyordernum+1)
gen lnweeklyrev = ln(weeklyrev+1)
gen lnweeklybnum = ln(weeklybnum+1)
gen lnweeklyquan = ln(weeklyquan+1)
gen lnweeklydnum = ln(weeklydnum+1)
gen weeklyqb = weeklyquan/weeklybnum
replace weeklyqb=0 if weeklyqb ==.
gen lnweeklyqb = ln(weeklyqb+1)
xtset productid week
gen l1lnweeklyrev = l.lnweeklyrev
gen dave_rating = d.ave_rating
gen dlnwtranprice = d.lnwtranprice
gen dlnweeklyrev =  l.lnweeklyrev -  l2.lnweeklyrev

gen treated = (week>37)
foreach i of numlist 1/5{
	gen treat_star`i'num = treated * star`i'num
	}

local keyvar "star1num star2num star3num star4num star5num"
xtset productid week
foreach k of numlist 1/1{
	foreach j of local keyvar{
		gen l`k'`j' = l`k'.`j'
		gen l`k'treat_`j' = l`k'.treat_`j'
		}
	}
egen ggroup = group(pgroup_use week)
est drop _all
local qualityvar "materialquality fguapro i.freturnpolicy lnsize lnitem lnpic"
local dep "rev quan qb bnum dnum"
//local dep "quan"
xtset pgroup_use
foreach j of local dep{
	foreach k of numlist 1/1{
	eststo peerresult`j':qui reghdfe lnweekly`j' lnwtranprice cat_ratedum* `qualityvar' i.week,absorb(pgroup_use sellerid) vce(cl productid)
	}
	}
estout peerresult*, drop(*.week) cells(b(star fmt(3)) se(par fmt(3))) stats(r2 N) starlevels(* 0.10 ** 0.05 *** 0.01)
